<!--
+----------------------------------------------------------------------
| 友得云客  - 开启房产营销新纪元
+----------------------------------------------------------------------
| Copyright (c) 2019~2023 优得（西安）信息科技有限公司版权所有
+----------------------------------------------------------------------
| Licensed 友得云客不是自由软件 未经允许不可移除相关版权
+----------------------------------------------------------------------
| Author: www.youdeyunke.com
+----------------------------------------------------------------------
-->
<template>
    <div  v-cloak>
        <el-form :loading="loading" label-width="120px" label-position="right" size="small" >
            <el-form-item label="客户姓名" required :disabled="!!item.id">
                <el-input show-word-limit maxlength="5"  v-model="item.name"></el-input>
            </el-form-item>
            <el-form-item label="客户性别" required>
                <el-radio v-model="item.sex" label="男性">男性</el-radio>
                <el-radio v-model="item.sex" label="女性">女性</el-radio>
                <el-radio v-model="item.sex" label="未知">未知</el-radio>
            </el-form-item>

            <el-form-item label="手机号" required>
                <el-input  v-model="item.mobile" size="small" ></el-input>
            </el-form-item>

            <el-form-item label="详情" required>
                <el-input show-word-limit type="textarea" maxlength="500" size="small"  :rows="6"
                    v-model="item.content"></el-input>
            </el-form-item>

            <el-form-item label="厂房" v-if="!pid" required>
                <post-selector v-model="item.post_id" size="small" ></post-selector>
            </el-form-item>

            <el-form-item label="状态" required>
                <status-selector custom-style="width:100%;" size="small" v-model="item.status_id" />
            </el-form-item>


            <el-form-item>
                <el-button @click="$emit('close')" size="mini" style="margin-left: 8px" icon="el-icon-close" >取消</el-button>
                <el-button :loading="loading" :disabled="!canSubmit" size="mini" type="primary" @click="submitHandle" icon="el-icon-check">确定</el-button>
            </el-form-item>
        </el-form>
    </div>
</template>

<script>
import { updateClue, createClue, getStatusList, getClueSourceTypeList } from "@/api/clue";
import StatusSelector from "./StatusSelector.vue";
import PostSelector from "@/components/PostSelector.vue";
export default {
    components: {
        StatusSelector,
        PostSelector
    },
    data () {
        return {
            show: false,
            statusItems: [],
            sourceTypes: [],
            loading: false,
        };
    },
    props: {
        item: { type: Object, default: {} },
        pid: { type: Number, default: 0 },
    },

    mounted: function () {
        this.loadData();
    },

    computed: {
        canSubmit: function () {
            var i = this.item
            if (!i.name) {
                return false
            }

            if (!i.mobile) {
                return false
            }

            if (!i.content) {
                return false
            }

            if (!i.status_id) {
                return false
            }

            if (!i.post_id && !this.pid) {
                return false
            }

            //if(!i.source_type){
            // return false
            //}
            return true
        },

        pageTitle: function () {
            return this.item.id ? "创建" : "编辑";
        },
    },

    methods: {
        sourceTypeSearch: function (k, cb) {
            if (this.sourceTypes.length > 0) {
                return cb(this.sourceTypes)
            }

            getClueSourceTypeList().then((resp) => {
                if (resp.status != 0) {
                    return
                }
                var items = resp.data.map((item, i) => {
                    return { value: item.name }
                })
                this.sourceTypes = items
                return cb(items)
            })
        },

        sourceTypeSelect: function (e) { },
        validate: function (item) {
            return true;
        },

        submitHandle: function () {
            var data = this.item;
            data.post_id = this.pid ? this.pid : data.post_id;
            var isok = this.validate(data);
            if (!isok) {
                return false;
            }

            this.loading = true;
            if (data.id) {
                updateClue(data).then((resp) => {
                    this.loading = false;
                    if (resp.status == 0) {
                        this.$message.success("保存成功");
                        this.$emit("success");
                    }
                });
                return;
            }
            createClue(data).then((resp) => {
                this.loading = false;
                if (resp.status == 0) {
                    this.$message.success("保存成功");
                    this.$emit("success");
                }
            });
        },

        loadData: function () {
            getStatusList().then((resp) => {
                if (resp.status != 0) {
                    return;
                }
                this.statusItems = resp.data;
            });
        },
    },
};
</script>

<style scoped>
 .footer{
    display: flex;
    /* width: 800px; */
    justify-content: right;
 }
</style>
